package com.caigou.modules.center.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.center.bean.*
import com.caigou.modules.center.repository.CenterRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/18
 */
@HiltViewModel
class DetailViewModel @Inject constructor(private val repository: CenterRepository) : ViewModel() {

    val myFollowLiveData = MutableLiveData<Resource<MyFollowBean>>()
    val myFansLiveData = MutableLiveData<Resource<MyFansBean>>()
    val removeFansLiveData = MutableLiveData<Resource<String>>()
    val flowerLiveData = MutableLiveData<Resource<FlowerBean>>()
    val renameCardLiveData = MutableLiveData<Resource<RenameCardBean>>()
    val deleteCardLiveData = MutableLiveData<Resource<DeleteCardBean>>()
    val freeCardLiveData = MutableLiveData<Resource<FreeCardBean>>()
    val myScoreDetailLiveData = MutableLiveData<Resource<MyScoreDetailBean>>()
    val scoreDetailLiveData = MutableLiveData<Resource<ScoreDetailBean>>()
    val forumDynamicLiveData = MutableLiveData<Resource<DynamicBean>>()
    val infoDynamicLiveData = MutableLiveData<Resource<DynamicBean>>()

    fun getMyFollowList(page: Long, limit: Long, userId: String) {
        myFollowLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getMyFollowList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MyFollowBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    myFollowLiveData.value = it
                }
        }
    }

    fun getMyFansList(page: Long, limit: Long, userId: String) {
        myFansLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getMyFansList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MyFansBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    myFansLiveData.value = it
                }
        }
    }

    fun removeFans(fansUserIds: MutableList<String>, masterUserId: String) {
        removeFansLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.removeFans(fansUserIds, masterUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    removeFansLiveData.value = it
                }
        }
    }

    fun getFlowerList(page: Long, limit: Long, userId: String) {
        flowerLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFlowerList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FlowerBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    flowerLiveData.value = it
                }
        }
    }

    fun getRenameCardList(page: Long, limit: Long, userId: String) {
        renameCardLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getRenameCardList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<RenameCardBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    renameCardLiveData.value = it
                }
        }
    }


    fun getDeleteCardList(page: Long, limit: Long, userId: String) {
        deleteCardLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getDeleteCardList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<DeleteCardBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    deleteCardLiveData.value = it
                }
        }
    }


    fun getFreeCardList(page: Long, limit: Long, userId: String) {
        freeCardLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFreeCardList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FreeCardBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    freeCardLiveData.value = it
                }
        }
    }


    fun getMyScoreDetail(userId: String) {
        myScoreDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getMyScoreDetail(userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MyScoreDetailBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    myScoreDetailLiveData.value = it
                }
        }
    }

    fun getScoreDetailList(page: Long, limit: Long, userId: String) {
        scoreDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getScoreDetailList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<ScoreDetailBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    scoreDetailLiveData.value = it
                }
        }
    }

    fun getForumDynamicList(feedType: Int, userId: String, contentType: Int) {
        forumDynamicLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getForumDynamicList(feedType, userId, contentType)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<DynamicBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    forumDynamicLiveData.value = it
                }
        }
    }

    fun getInfoDynamicList(feedType: Int, userId: String) {
        infoDynamicLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getInfoDynamicList(feedType, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<DynamicBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    infoDynamicLiveData.value = it
                }
        }
    }
}